#!/usr/bin/python3
# coding=utf-8
# Filename : UpdateDigkey.py
#Author:Bill Zhang
#Email:zchv@msn.com
#Digkey collection thread

from socket import timeout
import urllib.request
#import the BeautifulSoup which parse the html
from bs4 import BeautifulSoup
#from bs4.diagnose import diagnose
#import python's reguler expression lib
import re
class UpdateClass:
    """The UpdateClass:
        This class has the follow's method:
        1.getCode(self,url)
        2.toUpdate(self,HtmlCode,spplier)
    
        The detail of function is as follows:
            getCode:
                return the html code string
                or wrong code number when failed
            toUpdate:
                return the data list
    
    """
    def __init__(self):
        pass
    def getCode(self,url):    
        """The function getCode is to get the source code of the webpages"""
        
        user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"
        headers = {'User-agent':user_agent,'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language':'q=0.8,en-US;q=0.6,en;q=0.4'}
        req = urllib.request.Request(url=url,headers=headers)
        #HtmlCode save the source code of the webpages
        response="no cache"
        try:
            response = urllib.request.urlopen(req,timeout=20)
            print(response)
        except urllib.error.HTTPError as e:   
            print('Error code:',e.code)
            return e.code
        except urllib.error.URLError as e:   
            print('Reason',e.reason) 
            return 0
        except timeout:
            return 408
        HtmlCode=response.read().decode('utf-8') 
        return HtmlCode
        ##parse the html by using the BeautifulSoup

        ##f = open(URL[-7:], 'w') # open for 'w'riting
        ##f.write(HtmlCode) # write text to file
        ##f.close() # close the file

    def toUpdate(self,HtmlCode,supplier):
        """We use the BeautifulSoup to parse the html code.
        Some conversion as follow:
            eFind:the useful pattern in html code 
            qFind:the html's div specifier of price info
            qFind:the html's div specifier of quantity info
            pRE: the reguler pattern of Price
            qRE: the regular pattern of quantity
       The supplier map as follow:
            "Digikey"   :1
            "Mouser"    :2
            "future"    :3
        The data format as follow:
            list[goods_quantity,quantity1,price1,quantity2,price2......]
        """
        #print(HtmlCode)
        #re extract content digkey patternu 
        if(isinstance(HtmlCode,str)):
            SourceCode=BeautifulSoup(HtmlCode,"lxml")
        #if can't get the return code 
        #the quantity will be -1
        else:
            log=open("log/libupdate.log","a")
            log.write("Can not open the pages:%s" % str(HtmlCode))
            log.close()
            return [-1,0,0,0]
        #get product's quantity 
        #if supplier is 'Digikey':
        if(supplier==1):
            eFind=("table",'attrs={"class":"product-details-table"}')
            pFind=("table",'attrs={"id":"pricing"}')
            qFind=('td','attrs={"id":"quantityavailable"}')
            qRE=(r"\d+,*\d*")
            pRE=r"(?x)(\d*[\.0-9]\d*)"
       #if supplier is 'Mouser' 
        elif(supplier==2):
            pFind='"table", { "class" : "PriceBreaks" }'
            qFind='"table", { "id" : "ctl00_ContentMain_availability_tbl1" }'
            
        else:
            print("Can't find this supplier:%s" % supplier)
            print("Please contact Bill to add!")
            return -1
        #extract the useful part in html code
        usefulCode=SourceCode.find(eFind)
        if not usefulCode:
            print("Can't finde the info of %s" % eFind)
            return 0
        productInfo=usefulCode.extract() 

     #get product's quantity,if the number is not exsit,then the quantity will be set as 0;
        quantity=productInfo.find(qFind)
        if quantity is not None:
            quantity=quantity.text
            ProductQuantity=re.search(qRE,quantity)
            if ProductQuantity is not None:
                ProductQuantity=int(ProductQuantity.group(0).replace(',',''))
                
            else:
                ProductQuantity=0
        else:
            ProductQuantity=0
    #get product's price,if the price is not exsit,then the price will be set 0.
        price=productInfo.find(pFind)
        if price is not None:
            price=price.get_text("|",strip=True)
            price=price.replace(',','')
            ProductPrice=re.findall(pRE,price)
            if ProductPrice is not None:
                if(supplier==1):
                    ProductPrice=[k for i,k in enumerate(ProductPrice) if (i%3)!=2]
                if(supplier==2):
                    ProductPrice=self.flatten(ProductPrice)
            else:
                ProductPrice=[0,0]
        else:
            ProductPrice=[0,0]
        ProductPrice.insert(0,ProductQuantity)
        ProductPrice =map(float, ProductPrice)
        return ProductPrice
    def flatten(self,x):
        res=[]
        i=0
        while i<len(x):
            a=x[i][0]
            b=x[i][1]
            res.append(a)
            res.append(b)
            i=i+1
        return res
if __name__=="__main__":
    """
    ---if __name=="__main__"-----
    This is for debug this moudle,
    When run this moudle as the main 
    the code below will run
    """
    url="http://www.digikey.cn/product-detail/zh/SV21C201BJA01B00/490-4635-ND/1203914"
    a=UpdateClass()
    b=a.getCode(url)
    c=a.toUpdate(b,1)
    #type(abc)
    print(len(list(c)))
    print(list(c))

